﻿namespace HotSpot.Silverlight.Web.DomainServices
{
    using System.Data;    
    using System.Linq;
    using System.Web.DomainServices.Providers;
    using System.Web.Ria;
    using HotSpot.Silverlight.Web.DAL;
    using System;
    
    [EnableClientAccess()]
    public class HotSpotDomainService : LinqToEntitiesDomainService<HotSpotEntities>
    {
        #region Accommodation
        public IQueryable<Accommodation> GetAccommodation()
        {
            return this.ObjectContext.Accommodation;
        }

        public IQueryable<Accommodation> GetAccommodationSearch(
            string searchText, 
            int idCountry, 
            int idTown, 
            int Stars,
            bool detaliedSearchTags,
            bool allowPets,
            bool restaurant,
            bool spa,
            bool pool,
            bool parking,
            bool conferenceRoom)
        {
            IQueryable<Accommodation> acc =
                from p in this.ObjectContext.Accommodation.Include("Address.Town") select p;
           
            if (!String.IsNullOrEmpty(searchText))
                acc = from q in acc
                      where q.Name.Contains(searchText) || q.Description.Contains(searchText)
                      select q;

            if (idCountry > 0)
                acc = from q in acc where q.Address.Town.Country.Id == idCountry select q;

            if (idTown > 0)
                acc = from q in acc where q.Address.Town.Id == idTown select q;

            if (Stars > 0)
                acc = from q in acc where q.Stars == Stars select q;

            if (detaliedSearchTags)
                acc = from q in acc
                      where
                          q.Allow_Pets == allowPets &&
                          q.Restaurant == restaurant &&
                          q.Spa == spa &&
                          q.Pool == pool &&
                          q.Parking == parking &&
                          q.Conference_Room == conferenceRoom
                      select q;

            return acc;            
        }

        public void InsertAccommodation(Accommodation accommodation)
        {
            this.ObjectContext.AddToAccommodation(accommodation);
        }

        public void UpdateAccommodation(Accommodation currentAccommodation)
        {
            if ((currentAccommodation.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentAccommodation, this.ChangeSet.GetOriginal(currentAccommodation));
            }
        }

        public void DeleteAccommodation(Accommodation accommodation)
        {
            if ((accommodation.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(accommodation);
            }
            this.ObjectContext.DeleteObject(accommodation);
        }
        #endregion

        #region Address
        public IQueryable<Address> GetAddress()
        {
            return this.ObjectContext.Address;
        }

        public void InsertAddress(Address address)
        {
            this.ObjectContext.AddToAddress(address);
        }

        public void UpdateAddress(Address currentAddress)
        {
            if ((currentAddress.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentAddress, this.ChangeSet.GetOriginal(currentAddress));
            }
        }

        public void DeleteAddress(Address address)
        {
            if ((address.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(address);
            }
            this.ObjectContext.DeleteObject(address);
        }
        #endregion

        #region Comment
        public IQueryable<Comment> GetComment()
        {
            return this.ObjectContext.Comment;
        }

        public void InsertComment(Comment comment)
        {
            this.ObjectContext.AddToComment(comment);
        }

        public void UpdateComment(Comment currentComment)
        {
            if ((currentComment.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentComment, this.ChangeSet.GetOriginal(currentComment));
            }
        }

        public void DeleteComment(Comment comment)
        {
            if ((comment.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(comment);
            }
            this.ObjectContext.DeleteObject(comment);
        }
        #endregion

        #region Country
        public IQueryable<Country> GetCountry()
        {
            return this.ObjectContext.Country;
        }

        public void InsertCountry(Country country)
        {
            this.ObjectContext.AddToCountry(country);
        }

        public void UpdateCountry(Country currentCountry)
        {
            if ((currentCountry.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentCountry, this.ChangeSet.GetOriginal(currentCountry));
            }
        }

        public void DeleteCountry(Country country)
        {
            if ((country.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(country);
            }
            this.ObjectContext.DeleteObject(country);
        }
        #endregion

        #region Document
        public IQueryable<Document> GetDocument()
        {
            return this.ObjectContext.Document;
        }

        public void InsertDocument(Document document)
        {
            this.ObjectContext.AddToDocument(document);
        }

        public void UpdateDocument(Document currentDocument)
        {
            if ((currentDocument.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentDocument, this.ChangeSet.GetOriginal(currentDocument));
            }
        }

        public void DeleteDocument(Document document)
        {
            if ((document.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(document);
            }
            this.ObjectContext.DeleteObject(document);
        }
        #endregion

        #region Hit
        public IQueryable<Hit> GetHit()
        {
            return this.ObjectContext.Hit;
        }

        public void InsertHit(Hit hit)
        {
            this.ObjectContext.AddToHit(hit);
        }

        public void UpdateHit(Hit currentHit)
        {
            if ((currentHit.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentHit, this.ChangeSet.GetOriginal(currentHit));
            }
        }

        public void DeleteHit(Hit hit)
        {
            if ((hit.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(hit);
            }
            this.ObjectContext.DeleteObject(hit);
        }
        #endregion

        #region Room
        public IQueryable<Room> GetRoom()
        {
            return this.ObjectContext.Room;
        }

        public void InsertRoom(Room room)
        {
            this.ObjectContext.AddToRoom(room);
        }

        public void UpdateRoom(Room currentRoom)
        {
            if ((currentRoom.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentRoom, this.ChangeSet.GetOriginal(currentRoom));
            }
        }

        public void DeleteRoom(Room room)
        {
            if ((room.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(room);
            }
            this.ObjectContext.DeleteObject(room);
        }
        #endregion

        #region Town
        public IQueryable<Town> GetTown()
        {
            return this.ObjectContext.Town;
        }

        public IQueryable<Town> GetTownByIdCountry(int idCountry)
        {
            return this.ObjectContext.Town.Where(x => x.Country.Id.Equals(idCountry));
        }

        public void InsertTown(Town town)
        {
            this.ObjectContext.AddToTown(town);
        }

        public void UpdateTown(Town currentTown)
        {
            if ((currentTown.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentTown, this.ChangeSet.GetOriginal(currentTown));
            }
        }

        public void DeleteTown(Town town)
        {
            if ((town.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(town);
            }
            this.ObjectContext.DeleteObject(town);
        }
        #endregion

        #region User
        public IQueryable<User> GetUser()
        {
            return this.ObjectContext.User;
        }

        public void InsertUser(User user)
        {
            this.ObjectContext.AddToUser(user);
        }

        public void UpdateUser(User currentUser)
        {
            if ((currentUser.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentUser, this.ChangeSet.GetOriginal(currentUser));
            }
        }

        public void DeleteUser(User user)
        {
            if ((user.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(user);
            }
            this.ObjectContext.DeleteObject(user);
        }
        #endregion
    }
}


